package com.itbour.web.action.material;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.log.ErrorLogger;
import com.itbour.common.util.OSSUtil;
import com.itbour.common.util.StringUtils;
import com.itbour.def.MessageCode;
import com.itbour.def.MessageCodeConstants;
import com.itbour.def.MessageType;
import com.itbour.def.SystemConf;
import com.itbour.dto.com.FontInfoDto;
import com.itbour.dto.com.OSSTokenInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.material.FontService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright Itbour team 2015
 * 
 * @author xc
 */

public class GetFontListWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private FontService fontService;
	private List<FontInfoDto> fontInfoList;
	private FontInfoDto fontInfo;
	private String delIds;
	private String userId;
	private String findUserId;
	private String findFontName;
	private String findFileName;
	private String findPubFlg;
	private Map<String, String> uptoken;
	private OSSTokenInfoDto osstoken;
	
	HashMap<String, Object> mapResult = new HashMap<String, Object>(); 

	public String getFont() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("font_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("font_id", id);
		}

		fontInfoList = fontService.selectFontInfo(param);
		return SUCCESS;
	}
	
	public String getFontList() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(findUserId)) {
			param.put("user_id", findUserId);
		} else {
			if(!user.isAdmin()) {
				param.put("user_id", user.getUser_id());// 默认检索自己上传的字体
			}
		}
		if (StringUtils.isNotEmpty(findFontName)) {
			param.put("font_name", "%"+findFontName+"%");
		}
		if (StringUtils.isNotEmpty(findFileName)) {
			param.put("file_name", "%"+findFileName+"%");
		}
		if (StringUtils.isNotBlank(findPubFlg)) {
			param.put("publish", findPubFlg);
		}
		Page page = fontService.pageQuery(param, Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		mapResult = resMap;
		return SUCCESS;
	}
	
	public void deleteFont() throws Exception {
		fontService.deleteFontInfo(delIds);
		this.writeJSON("ok");
	}

	public void addFont() throws Exception {
		if (fontInfo != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		if (user.getUser_name() != null) {
	    			fontInfo.setUpdate_by(user.getUser_name());
	    		} else {
	    			fontInfo.setUpdate_by(user.getUser_id().toString());
	    		}
	    		fontInfo.setUser_id(user.getUser_id());
	    	}
			fontService.addFont(fontInfo);
			this.writeJSON("ok");
		} else {
			this.writeJSON("ng");
		}
	}
	
	public void updFont() throws Exception {
		if (fontInfo != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		if (user.getUser_name() != null) {
	    			fontInfo.setUpdate_by(user.getUser_name());
	    		} else {
	    			fontInfo.setUpdate_by(user.getUser_id().toString());
	    		}
	    	}
			fontService.updFont(fontInfo);
		}
		this.writeJSON("ok");
	}
	
	public void chkFontName() throws Exception {
		if (fontInfo != null) {
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	if (fontInfo.getFont_id() != null) {
	    		param.put("font_id", fontInfo.getFont_id());
	    	} else {
	    		param.put("font_name", fontInfo.getFont_name());
	    	}
	    	int cnt = fontService.chkFontName(param);
			if(cnt==0) {
				this.writeJSON("ok");
				return;
			}
		}
		this.writeJSON("ng");
	}

	public String getOSSToken() throws Exception {
		OSSUtil ossUtil = new OSSUtil();
		osstoken = ossUtil.getToken(0);
		return SUCCESS;
	}
	
	public String fontInit() {
		return SUCCESS;
	}

	public FontService getFontService() {
		return fontService;
	}

	public void setFontService(FontService fontService) {
		this.fontService = fontService;
	}

	public List<FontInfoDto> getFontInfoList() {
		return fontInfoList;
	}

	public void setFontInfoList(List<FontInfoDto> fontInfoList) {
		this.fontInfoList = fontInfoList;
	}

	public FontInfoDto getFontInfo() {
		return fontInfo;
	}

	public void setFontInfo(FontInfoDto fontInfo) {
		this.fontInfo = fontInfo;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Map<String, String> getUptoken() {
		return uptoken;
	}

	public void setUptoken(Map<String, String> uptoken) {
		this.uptoken = uptoken;
	}

	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}

	public String getFindUserId() {
		return findUserId;
	}

	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}

	public String getFindFontName() {
		return findFontName;
	}

	public void setFindFontName(String findFontName) {
		this.findFontName = findFontName;
	}

	public String getFindFileName() {
		return findFileName;
	}

	public void setFindFileName(String findFileName) {
		this.findFileName = findFileName;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public OSSTokenInfoDto getOsstoken() {
		return osstoken;
	}

	public void setOsstoken(OSSTokenInfoDto osstoken) {
		this.osstoken = osstoken;
	}

}
